<template>
  <view class="goods_list">
    <view v-for="(good, i) in goodsList" :key="i" @click="gotoDetail(good)"><my-goods :good="good"></my-goods></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      queryObj: {
        query: '',
        cid: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      flag: false
    };
  },
  onLoad(options) {
    this.queryObj.query = options.query || '';
    this.queryObj.cid = options.cid || '';
    this.getGoodsList();
  },
  onPullDownRefresh() {
    this.queryObj.pagenum = 1;
    this.total = 0;
    this.flag = false;
    this.goodsList = [];
    this.getGoodsList(() => uni.stopPullDownRefresh());
  },
  onReachBottom() {
    const { pagenum, pagesize } = this.queryObj;
    if (this.flag) return;
    if (pagenum * pagesize >= this.total) return uni.$showMsg('已经到底啦');
    this.queryObj.pagenum++;
    this.getGoodsList();
  },
  methods: {
    async getGoodsList(cb) {
      this.flag = true;
      const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
      cb && cb();
      this.flag = false;
      if (res.meta.status !== 200) return uni.$showMsg();
      res.message.goods.forEach(i => {
        i.price = i.goods_price.toFixed(2);
      });
      this.goodsList = [...this.goodsList, ...res.message.goods];
      this.total = res.message.total;
    },
    gotoDetail(good) {
      uni.navigateTo({
        url: '/subpkg/goods_detail/goods_detail?goods_id=' + good.goods_id
      });
    }
  }
};
</script>

<style lang="scss"></style>
